import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'index.dart';

class DiscoverDetialPage extends GetView<DiscoverDetialController> {
  const DiscoverDetialPage({super.key});

  // 主视图
  Widget _buildView() {
    return CustomScrollView(
      slivers: [
        // 用户信息卡片
        SliverToBoxAdapter(
          child: _buildUserCard(),
        ),

        // 任务目标
        SliverToBoxAdapter(
          child: _buildSection(
            title: '任务目标',
            child: _buildTaskTarget(),
          ),
        ),

        // 任务信息
        SliverToBoxAdapter(
          child: _buildSection(
            title: '任务信息',
            child: _buildTaskInfo(),
          ),
        ),

        // 任务奖励
        SliverToBoxAdapter(
          child: _buildSection(
            title: '任务奖励',
            child: _buildRewardInfo(),
          ),
        ),

        // 任务要求
        SliverToBoxAdapter(
          child: _buildSection(
            title: '任务要求',
            child: _buildTaskRequirement(),
          ),
        ),

        // 底部安全区域
        SliverToBoxAdapter(
          child: SizedBox(height: 80.h),
        ),
      ],
    );
  }

  // 用户信息卡片
  Widget _buildUserCard() {
    return Container(
      margin: EdgeInsets.all(12.r),
      padding: EdgeInsets.all(12.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Obx(() {
        final userName = controller.taskData['userName'] ?? '好的好的好';
        final avatar = controller.taskData['avatar'] ?? '';
        final taskTitle = controller.taskData['taskTitle'] ?? '测试商单全返回2';
        final createTime = controller.taskData['createTime'] ?? '2025-11-06 15:28:41';

        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                // 头像
                ClipOval(
                  child: avatar.isNotEmpty
                      ? Image.network(
                          avatar,
                          width: 48.w,
                          height: 48.h,
                          fit: BoxFit.cover,
                          errorBuilder: (context, error, stackTrace) {
                            return _buildDefaultAvatar();
                          },
                        )
                      : _buildDefaultAvatar(),
                ),
                SizedBox(width: 10.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        userName,
                        style: TextStyle(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.bold,
                          color: const Color(0xFF2C3E50),
                        ),
                      ),
                      SizedBox(height: 4.h),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 2.h),
                        decoration: BoxDecoration(
                          color: Colors.blue[50],
                          borderRadius: BorderRadius.circular(4.r),
                        ),
                        child: Text(
                          '私信',
                          style: TextStyle(
                            fontSize: 10.sp,
                            color: Colors.blue[700],
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 10.h),
            Text(
              taskTitle,
              style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.w600,
                color: const Color(0xFF2C3E50),
              ),
            ),
            SizedBox(height: 4.h),
            Text(
              createTime,
              style: TextStyle(
                fontSize: 11.sp,
                color: const Color(0xFF95A5A6),
              ),
            ),
          ],
        );
      }),
    );
  }

  // 默认头像
  Widget _buildDefaultAvatar() {
    return Container(
      width: 48.w,
      height: 48.h,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        gradient: LinearGradient(
          colors: [
            Color(0xFF667EEA),
            Color(0xFF764BA2),
          ],
        ),
      ),
      child: Icon(
        Icons.person,
        color: Colors.white,
        size: 28.r,
      ),
    );
  }

  // 区块容器
  Widget _buildSection({required String title, required Widget child}) {
    return Container(
      margin: EdgeInsets.fromLTRB(12.r, 0, 12.r, 12.r),
      padding: EdgeInsets.all(12.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                width: 3.w,
                height: 14.h,
                decoration: BoxDecoration(
                  color: const Color(0xFFFF6B35),
                  borderRadius: BorderRadius.circular(2.r),
                ),
              ),
              SizedBox(width: 6.w),
              Text(
                title,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.bold,
                  color: const Color(0xFF2C3E50),
                ),
              ),
            ],
          ),
          SizedBox(height: 10.h),
          child,
        ],
      ),
    );
  }

  // 任务目标
  Widget _buildTaskTarget() {
    return Obx(() {
      final target = controller.taskData['target'] ?? '1';
      return Text(
        target,
        style: TextStyle(
          fontSize: 12.sp,
          color: const Color(0xFF34495E),
        ),
      );
    });
  }

  // 任务信息
  Widget _buildTaskInfo() {
    return Obx(() {
      final deadline = controller.taskData['deadline'] ?? '2025-11-07 23:59:59';
      final completeDays = controller.taskData['completeDays'] ?? '1天';
      final taskCount = controller.taskData['taskCount'] ?? '10';

      return Column(
        children: [
          _buildInfoRow('截止时间:', deadline),
          SizedBox(height: 8.h),
          _buildInfoRow('完成时限:', completeDays),
          SizedBox(height: 8.h),
          _buildInfoRow('任务数量:', taskCount),
        ],
      );
    });
  }

  // 奖励信息
  Widget _buildRewardInfo() {
    return Obx(() {
      final taskReward = controller.taskData['taskReward'] ?? '100';
      final adReward = controller.taskData['adReward'] ?? '100';

      return Column(
        children: [
          _buildInfoRow('任务奖励:', '$taskReward金币', valueColor: const Color(0xFFFF6B35)),
          SizedBox(height: 8.h),
          _buildInfoRow('广告运维师奖励:', '$adReward金币', valueColor: const Color(0xFFFF6B35)),
        ],
      );
    });
  }

  // 任务要求
  Widget _buildTaskRequirement() {
    return Obx(() {
      final requirement = controller.taskData['requirement'] ?? '1';
      return Text(
        requirement,
        style: TextStyle(
          fontSize: 12.sp,
          color: const Color(0xFF34495E),
        ),
      );
    });
  }

  // 信息行
  Widget _buildInfoRow(String label, String value, {Color? valueColor}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 90.w,
          child: Text(
            label,
            style: TextStyle(
              fontSize: 12.sp,
              color: const Color(0xFF95A5A6),
            ),
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: TextStyle(
              fontSize: 12.sp,
              color: valueColor ?? const Color(0xFF2C3E50),
              fontWeight: valueColor != null ? FontWeight.bold : FontWeight.normal,
            ),
          ),
        ),
      ],
    );
  }

  // 底部按钮栏
  Widget _buildBottomBar() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.h),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8.r,
            offset: Offset(0, -2.h),
          ),
        ],
      ),
      child: SafeArea(
        child: Row(
          children: [
            // 分享按钮
            IconButton(
              onPressed: controller.onShare,
              icon: Icon(
                Icons.share,
                size: 24.r,
                color: const Color(0xFF95A5A6),
              ),
            ),
            SizedBox(width: 8.w),
            // 接单按钮
            Expanded(
              child: ElevatedButton(
                onPressed: controller.onTakeOrder,
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFFFF6B35),
                  minimumSize: Size(0, 44.h),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(22.r),
                  ),
                ),
                child: Text(
                  '我要接单',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DiscoverDetialController>(
      init: DiscoverDetialController(),
      id: "discover_detial",
      builder: (_) {
        return Scaffold(
          backgroundColor: const Color(0xFFF5F5F5),
          appBar: AppBar(
            backgroundColor: Colors.white,
            elevation: 0,
            leading: IconButton(
              icon: Icon(
                Icons.arrow_back_ios,
                size: 20.r,
                color: const Color(0xFF2C3E50),
              ),
              onPressed: () => Get.back(),
            ),
            title: Text(
              '商单详细',
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.bold,
                color: const Color(0xFF2C3E50),
              ),
            ),
            centerTitle: true,
            actions: [
              IconButton(
                icon: Icon(
                  Icons.more_vert,
                  size: 24.r,
                  color: const Color(0xFF2C3E50),
                ),
                onPressed: () {},
              ),
            ],
          ),
          body: _buildView(),
          bottomNavigationBar: _buildBottomBar(),
        );
      },
    );
  }
}
